package cn.cdutetc.library.controller;

import cn.cdutetc.library.common.ApiResponse;
import cn.cdutetc.library.entity.dto.SystemParamConfigDTO;
import cn.cdutetc.library.entity.pojo.SystemConfig;
import cn.cdutetc.library.service.SystemConfigService;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 系统配置控制器
 */
@RestController
@RequestMapping("/api/system/config")
@Tag(name = "系统配置接口")
public class SystemConfigController {

    @Autowired
    private SystemConfigService systemConfigService;

    @GetMapping("/{configKey}")
    @Operation(summary = "获取配置值")
    @Parameter(name = "configKey", description = "配置键", required = true)
    public ApiResponse<String> getConfigValue(@PathVariable String configKey) {
        String value = systemConfigService.getConfigValue(configKey);
        return ApiResponse.success(value);
    }

    @PostMapping("/set")
    @Operation(summary = "设置配置值")
    @Parameter(name = "configKey", description = "配置键", required = true)
    @Parameter(name = "configValue", description = "配置值", required = true)
    @Parameter(name = "description", description = "配置描述", required = false)                
    public ApiResponse<Boolean> setConfigValue(
            @RequestParam @Parameter(description = "配置键") String configKey,
            @RequestParam @Parameter(description = "配置值") String configValue,
            @RequestParam(required = false) @Parameter(description = "配置描述") String description) {
        boolean result = systemConfigService.setConfigValue(configKey, configValue, description);
        return ApiResponse.success(result);
    }

    @PostMapping("/batch")
    @Operation(summary = "批量设置配置")
    public ApiResponse<Map<String, Object>> batchSetConfig(@RequestBody Object configs) {
        Map<String, String> configMap = new HashMap<>();
        
        // 处理两种可能的输入格式：Map<String, String> 或 List<SystemConfig>
        if (configs instanceof Map) {
            configMap = (Map<String, String>) configs;
        } else if (configs instanceof List) {
            List<?> configList = (List<?>) configs;
            for (Object item : configList) {
                if (item instanceof Map) {
                    Map<String, Object> configItem = (Map<String, Object>) item;
                    String key = (String) configItem.get("configKey");
                    String value = (String) configItem.get("configValue");
                    if (key != null && value != null) {
                        configMap.put(key, value);
                    }
                }
            }
        }
        
        boolean result = systemConfigService.batchSetConfig(configMap);
        Map<String, Object> response = new HashMap<>();
        response.put("success", result);
        if (result) {
            response.put("message", "配置更新成功");
        } else {
            response.put("message", "配置更新失败");
        }
        return ApiResponse.success(response);
    }

    @DeleteMapping("/{configKey}")
    @Operation(summary = "删除配置")
    @Parameter(name = "configKey", description = "配置键", required = true)
    public ApiResponse<Boolean> deleteConfig(@PathVariable String configKey) {
        boolean result = systemConfigService.deleteConfig(configKey);
        return ApiResponse.success(result);
    }

    @GetMapping("/all")
    @Operation(summary = "获取所有配置")
    public ApiResponse<List<SystemConfig>> getAllConfigs() {
        List<SystemConfig> configs = systemConfigService.getAllConfigs();
        return ApiResponse.success(configs);
    }

    @GetMapping("/system")
    @Operation(summary = "获取系统配置")
    public ApiResponse<SystemParamConfigDTO> getSystemConfig() {
        SystemParamConfigDTO config = systemConfigService.getSystemConfig();
        return ApiResponse.success(config);
    }

    @PostMapping("/system")
    @Operation(summary = "保存系统配置")
    public ApiResponse<Boolean> saveSystemConfig(@RequestBody SystemParamConfigDTO config) {
        boolean result = systemConfigService.saveSystemConfig(config);
        return ApiResponse.success(result);
    }

    @PostMapping("/reset")
    @Operation(summary = "恢复默认配置")
    public ApiResponse<Boolean> resetSystemConfig() {
        boolean result = systemConfigService.resetSystemConfig();
        return ApiResponse.success(result);
    }

    @GetMapping("/maintenance/status")
    @Operation(summary = "检查维护模式状态")
    public ApiResponse<Map<String, Object>> checkMaintenanceMode() {
        boolean maintenanceMode = systemConfigService.isMaintenanceMode();
        Map<String, Object> result = new HashMap<>();
        result.put("success", true);
        result.put("maintenanceMode", maintenanceMode);
        return ApiResponse.success(result);
    }

    @PostMapping("/maintenance/disable")
    @Operation(summary = "验证维护密码并关闭维护模式")
    public ApiResponse<Map<String, Object>> verifyMaintenancePassword(@RequestBody Map<String, String> request) {
        String password = request.get("password");
        boolean result = systemConfigService.verifyMaintenancePassword(password);
        Map<String, Object> response = new HashMap<>();
        response.put("success", result);
        if (result) {
            response.put("message", "维护模式已关闭");
        } else {
            response.put("message", "密码错误");
        }
        return ApiResponse.success(response);
    }


}